//
//  vdView.m
//  Void Dead
//
//  Created by Sidney Just on 16.11.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdView.h"
#import "vdKernel.h"

@implementation vdView
@synthesize backingWidth, backingHeight, deltaTime, clearColor;
@synthesize orientation, viewFramebuffer;


+ (Class)layerClass {
    return [CAEAGLLayer class];
}

#pragma mark -
#pragma mark Rendering

- (void)applyOrientation { 
}

- (void)prepareRendering {
}

- (void)completeRendering {
}

- (void)bindFramebuffer {
}

#pragma mark -
#pragma mark Touches

- (void)touchesBegan:(NSSet *)Atouches withEvent:(UIEvent *)event {
	NSSet *allTouches = [event allTouches];
	
	oldTouchesCount = [[allTouches allObjects] count];
	oldTouches = (CGPoint *)malloc(oldTouchesCount*sizeof(CGPoint));
	
	vdInputMessage message;
	message.event = vdTouchesBegan;
	message.touchCount = [[allTouches allObjects] count];
	
	message.touch = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	message.diff = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	
	for(int i=0; i<[[allTouches allObjects] count]; i++)
	{
		UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
		message.touch[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		message.diff[i] = CGPointMake(message.touch[i].x - oldTouches[i].x, message.touch[i].y - oldTouches[i].y);
		oldTouches[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		
		message.touch[i] = [[vdKernel sharedKernel] convertUIToOpenGL:message.touch[i]];
		message.diff[i] = [[vdKernel sharedKernel] applyOrientation:message.diff[i]];
	}
	
	
	[[vdEventHandler sharedEventHandler] fireEvent:vdInputEvent sender:self data:&message];
	
	free(message.touch);
	free(message.diff);
}


- (void)touchesMoved:(NSSet *)Atouches withEvent:(UIEvent *)event {
	NSSet *allTouches = [event allTouches];
	
	oldTouchesCount = [[allTouches allObjects] count];
	oldTouches = (CGPoint *)realloc(oldTouches, oldTouchesCount*sizeof(CGPoint));
	
	vdInputMessage message;
	message.event = vdTouchesMoved;
	message.touchCount = [[allTouches allObjects] count];
	
	message.touch = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	message.diff = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	
	for(int i=0; i<[[allTouches allObjects] count]; i++)
	{
		UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
		message.touch[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		message.diff[i] = CGPointMake(message.touch[i].x - oldTouches[i].x, message.touch[i].y - oldTouches[i].y);
		oldTouches[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		
		message.touch[i] = [[vdKernel sharedKernel] convertUIToOpenGL:message.touch[i]];
		message.diff[i] = [[vdKernel sharedKernel] applyOrientation:message.diff[i]];
	}
	
	
	[[vdEventHandler sharedEventHandler] fireEvent:vdInputEvent sender:self data:&message];
	
	free(message.touch);
	free(message.diff);
}

- (void)touchesEnded:(NSSet *)Atouches withEvent:(UIEvent *)event {
	NSSet *allTouches = [event allTouches];
	
	oldTouchesCount = [[allTouches allObjects] count];
	oldTouches = (CGPoint *)realloc(oldTouches, oldTouchesCount*sizeof(CGPoint));
	
	vdInputMessage message;
	message.event = vdTouchesEnded;
	message.touchCount = [[allTouches allObjects] count];
	
	message.touch = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	message.diff = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	
	for(int i=0; i<[[allTouches allObjects] count]; i++)
	{
		UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
		message.touch[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		message.diff[i] = CGPointMake(message.touch[i].x - oldTouches[i].x, message.touch[i].y - oldTouches[i].y);
		oldTouches[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		
		message.touch[i] = [[vdKernel sharedKernel] convertUIToOpenGL:message.touch[i]];
		message.diff[i] = [[vdKernel sharedKernel] applyOrientation:message.diff[i]];
	}
	
	[[vdEventHandler sharedEventHandler] fireEvent:vdInputEvent sender:self data:&message];
	
	free(message.touch);
	free(message.diff);
	
	free(oldTouches);
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	NSSet *allTouches = [event allTouches];
	
	oldTouchesCount = [[allTouches allObjects] count];
	oldTouches = (CGPoint *)realloc(oldTouches, oldTouchesCount*sizeof(CGPoint));
	
	vdInputMessage message;
	message.event = vdTouchesCancelled;
	message.touchCount = [[allTouches allObjects] count];
	
	message.touch = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	message.diff = (CGPoint *)malloc(message.touchCount*sizeof(CGPoint));
	
	for(int i=0; i<[[allTouches allObjects] count]; i++)
	{
		UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
		message.touch[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		message.diff[i] = CGPointMake(message.touch[i].x - oldTouches[i].x, message.touch[i].y - oldTouches[i].y);
		oldTouches[i] = CGPointMake([touch locationInView:self].x, [touch locationInView:self].y);
		
		message.touch[i] = [[vdKernel sharedKernel] convertUIToOpenGL:message.touch[i]];
		message.diff[i] = [[vdKernel sharedKernel] applyOrientation:message.diff[i]];
	}
	
	
	[[vdEventHandler sharedEventHandler] fireEvent:vdInputEvent sender:self data:&message];
	
	free(message.touch);
	free(message.diff);
	
	free(oldTouches);
}



#pragma mark -
#pragma mark Con- Destructor

- (id)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame])		
	{
		clearColor = (vdColor *)malloc(sizeof(vdColor));
		
		clearColor->alpha = 1.0f;
		clearColor->red   = 0.0f;
		clearColor->green = 0.0f;
		clearColor->blue  = 0.5f;
	}
	return self;
}

- (void)dealloc {
	free(clearColor);
	
    [super dealloc];
}


@end
